package day04;

/*
@Author: 王少杰
@Date: 2025/1/3
@Time: 9:22
@FileName: Demo02.java
@描述:
多态:对于子类的对象来说，它有多中形态，既是子类类型的，又是父类类型的。
*/
public class Demo02 {
    public static void main(String[] args) {
        Pet pet = new Pet();
        pet.setName("小明");
        pet.setAge(18);
        pet.setHealth(60);
        System.out.println(pet);

        Fish fish = new Fish();
        fish.setName("小黄");
        fish.setAge(1);
        fish.setHealth(80);
        System.out.println(fish);

        Dog dog = new Dog();
        dog.setName("大黄");
        dog.setAge(8);
        dog.setHealth(90);
        dog.setPinzhong("中华田园犬");
        System.out.println(dog);

        doctor(pet);
        System.out.println(pet);
        doctor(fish);
        System.out.println(fish);
        doctor(dog);
        System.out.println(dog);

        //多态的体现。Python中不明显
        Pet pet1 = new Pet("cat",1,50);
        doctor(pet1);
        System.out.println(pet1);
    }

    public static void doctor(Pet pet){ //Pet 类型的参数，调用doctor时可以传Pet以及Pet的子类的对象。
        if (pet.getHealth() < 60){
            System.out.println("打针输液");
            pet.setHealth(pet.getHealth() + 60);
        }else {
            System.out.println("吃药");
            pet.setHealth(pet.getHealth() + 5);
        }
    }
}